package com.jiandian.page;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Map;

/**
 *
 * @author lik
 * @date 2017/4/14
 */
@Getter
@Setter
public class PageInfo {
    private String filterSql;

    /**
     * 当前页
     */
    private Integer currentPage;
    /**
     * 分组
     */
    private String groupColumns;
    /**
     * 排序字段
     */
    private String orderColumn;
    /**
     * 升/降序排列
     */
    private String orderSort;
    /**
     * 一页多少条数据
     */
    private Integer pageSize;
    /**
     * 返回对象
     */
    private Object resultObject;
    /**
     * limit sql起始数值(由currentPage计算得出)
     */
    private Integer startRecord;
    /**
     * 总页数(由totalRecord计算得出)
     */
    private Integer totalPages;
    /**
     * 总条数
     */
    private Integer totalRecord;
    /**
     * where 查询
     */
    private Map<String, Object> whereMap;

    public void queryPageResult(PageInfo pageInfo) {
        if (StringUtils.isEmpty(pageInfo.getOrderColumn())) {
            pageInfo.setOrderColumn("id");
        }
        if (StringUtils.isEmpty(pageInfo.getOrderSort())) {
            pageInfo.setOrderSort("desc");
        }
        if (pageInfo.getPageSize() == null) {
            pageInfo.setPageSize(10);
        }
        if (pageInfo.getStartRecord() == null) {
            pageInfo.setStartRecord(0);
        }
        if (pageInfo.getTotalPages() == null) {
            pageInfo.setTotalPages(pageInfo.getTotalRecord() / pageInfo.getPageSize() + 1);
        }

        pageInfo.setCurrentPage(pageInfo.getStartRecord() / pageInfo.getPageSize() + 1);
    }

    public Integer getStartRecord() {
        if (null == getCurrentPage()) {
            return 0;
        }
        return (getCurrentPage() - 1) * pageSize;
    }

    @Override
    public String toString() {
        StringBuilder whereMapBuffer = new StringBuilder();
        if (whereMap != null) {
            for (Map.Entry<String, Object> entry : whereMap.entrySet()) {
                if (entry.getKey().equals("branchIdArray") || entry.getKey().equals("orderStatusArray")) {
                    whereMapBuffer.append(entry.getKey() + ":" + Arrays.toString((String[]) entry.getValue()) + ",");
                } else {
                    whereMapBuffer.append(entry.getKey() + ":" + entry.getValue() + ",");
                }
            }
        }

        return new StringBuilder()
                .append("startRecord:" + startRecord + ",")
                .append("currentPage:" + currentPage + ",")
                .append("pageSize:" + pageSize + ",")
                .append("totalPages:" + totalPages + ",")
                .append("totalRecord:" + totalRecord + ",")
                .append("orderColumn:" + orderColumn + ",")
                .append("orderSort:" + orderSort + ",")
                .append("groupColumns:" + groupColumns + ",")
                .append("whereMap:{" + whereMapBuffer + "},")
                .append("resultObject:" + resultObject)
                .toString();
    }
}
